﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace FeatureModelCore.FMShapes
{
    public class FMRectangle
    {
        public readonly static string _XML_WIDTH = "Width";
        public readonly static string _XML_HEIGHT = "Height";

        public double Width { get; internal set; }
        public double Height { get; internal set; }
        public FMPoint Center { get; internal set; }

        public FMRectangle()
        {
            Width = -1;
            Height = -1;
            Center = new FMPoint();
        }

        public FMRectangle(double width, double height, FMPoint center)
        {
            Width = width;
            Height = height;
            Center = center;
        }

        #region XML Parser
        /// <summary>
        /// To Xml Element
        /// </summary>
        /// <param name="xelem"></param>
        public void ToXElem(XElement xelem)
        {
            Center.ToXElem(xelem);
            xelem.Add(new XAttribute(_XML_HEIGHT, Height));
            xelem.Add(new XAttribute(_XML_WIDTH, Width));
        }
        /// <summary>
        /// Parse Xelement to FMCircle
        /// </summary>
        /// <param name="xelem"></param>
        /// <returns></returns>
        public static FMRectangle ParseXElem(XElement xelem)
        {
            FMPoint center = FMPoint.ParseXElem(xelem);
            double width = -1;
            double height = -1;

            XAttribute xWidth = xelem.Attribute(_XML_WIDTH);
            XAttribute xHeight = xelem.Attribute(_XML_HEIGHT);
            if (xWidth != null)
            {
                width = double.Parse(xWidth.Value);
            }
            if (xHeight != null)
            {
                height = double.Parse(xHeight.Value);
            }
            return new FMRectangle(width, height, center);
        }
        #endregion
    }
}
